package overlays;

import java.util.ArrayList;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

/**
 * This class is used to draw an overlay of a route inside  onto a given google mapview,
 * based on a set of directions given as an ArrayList of GeoPoints.
 * 
 * @author Nick
 * @version 1
 **/
public class RouteOverlay extends Overlay {
	private ArrayList<GeoPoint> route;

	/**
	 * A constructor for this overlay.
	 * @param route the ArrayList of GeoPoints for the route to be overlaid.
	 */
	public RouteOverlay(ArrayList<GeoPoint> route) {
		super();		
		this.route = route;
	}

	@Override
	public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
		super.draw(canvas, mv, shadow);
		drawPath(mv, canvas);
		return true;
	}

	/**
	 * This method draws a path on a given MapView.
	 * @param mv the MapView that the route will be drawn onto.
	 * @param canvas the canvas which contains the MapView.
	 */
	public void drawPath(MapView mv, Canvas canvas) {
		int lastX = -1, yPrev = -1, xNow = -1, yNow = -1;
		
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(Color.YELLOW);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		paint.setStrokeWidth(5);

		if (route != null) {
			for (int i = 0; i < route.size(); i++) {
				GeoPoint gp = route.get(i);
				Point point = new Point();
				mv.getProjection().toPixels(gp, point);
				xNow = point.x;
				yNow = point.y;
				if (lastX != -1)
					canvas.drawLine(lastX, yPrev, xNow, yNow, paint);
				lastX = xNow;
				yPrev = yNow;
			}
		}
	}
}